package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bminfo.migle.parser.GetTarget;
import org.bminfo.migle.parser.PointMut;
import org.bminfo.migle.parser.Visualization;
import org.bminfo.migle.wrapper.MiRandaWrapper;
import org.bminfo.sequence.FastaSeq;

public class SNPServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SNPServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String folderName = request.getSession().getAttribute("folderName").toString();
        String miGLE_home = request.getSession().getAttribute("miGLE_home").toString();
		String site = request.getSession().getAttribute("site").toString();
		
        String iupac_char = request.getSession().getAttribute("iupac_char").toString();
        String method = request.getParameter("method");
		String point = request.getParameter("point");
        
        String delta = new String();
        
    	delta = request.getParameter("delta");
    	if(delta == null){
    		System.out.println("delta = null");
    		delta = "0";
    	}
    	
    	request.getSession().setAttribute("delta", delta);
        
//		convert 
		PointMut pm = new PointMut();
		pm.setPointMutPath(miGLE_home, folderName);
		
		String infile = pm.tempPath + "seq.fasta";
		
		FastaSeq fasta = new FastaSeq(infile, "DNA");
		String seq = fasta.GetFirstSequence().seqString();
		String title = fasta.GetFirstSequence().getName();
		
		System.out.println(title);
		System.out.println(seq);
		System.out.println(pm.tempPath);
		System.out.println(infile);
		
		HashMap<String, String> snpInfo = PointMut.getAllelesType(title);
		
		pm.buildAnalysisFile(pm.tempPath, title, seq, snpInfo, point);
        
		
		if(method.equals("miranda")){
			String go = request.getParameter("go");
			String ge = request.getParameter("ge");
			String en = request.getParameter("en");
			String sc = request.getParameter("sc");
			
			String parameter = "-go " + go + " -ge " + ge + " -en " + en + " -sc " + sc;
            			
			pm.setPointMutPath(miGLE_home, folderName);
			
			MiRandaWrapper mr = new MiRandaWrapper();
			mr.setMirandaPath(miGLE_home, folderName);
			HashMap<String, Double> RankedmiRNA = mr.getRankedLossEvent(miGLE_home, mr.tempPath, infile, point, parameter, snpInfo);
						
//			StringBuilder delta_rlt = new StringBuilder();
//			
//			for(String miRNA: RankedmiRNA.keySet()){
//				delta_rlt.append(miRNA + "\t" + RankedmiRNA.get(miRNA) + "<br>");
//				System.out.println(miRNA + "\t" + RankedmiRNA.get(miRNA));
//			}
			
            request.getSession().setAttribute("folderName", folderName);
			
			Visualization visual = new Visualization();
			
			if(delta.equals("1")){
				String result;
				try {
					result = visual.deltaTable(RankedmiRNA, pm.moopPath);
		            request.getSession().setAttribute("result", result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}else{
				String result;
				try {
					result = visual.gleTable(mr, snpInfo, snpInfo.get("alleles") , point);
					request.getSession().setAttribute("result", result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
            
    		response.sendRedirect("../snp.jsp");
            
		}else{
            
    		response.sendRedirect("../index.jsp");
		}    

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}
